package com.medicine.hbp.ui;

import org.kymjs.kjframe.http.HttpParams;
import org.kymjs.kjframe.ui.BindView;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

import com.medicine.hbp.adapter.WonderfulAppAdapter;
import com.medicine.hbp.base.BaseActivity;
import com.medicine.hbp.http.AppHttpCallBack;
import com.medicine.hbp.http.AppHttps;
import com.medicine.hbp.http.HttpInterface;
import com.medicine.hbp.mob.findByListApplyListVo;
import com.medicine.hbp.uitl.CommUtil;
import com.medicine.hbp.uitl.NetWorkUtils;
import com.medicine.hbp.uitl.ToastUtil;
import com.medicine.hbp.xListView.XListView;
import com.medicine.hbp.xListView.XListView.IXListViewListener;
import com.medicine.hbp.R;

public class WonderfulAppActivity extends BaseActivity implements IXListViewListener {
	@BindView(id = R.id.xListView)
	public XListView xlistView;
	@BindView(id = R.id.image_no_wifi) public ImageView NoNetWork;
	private WonderfulAppAdapter adapter;
	private volatile boolean refresh = true;
	private volatile int page = 1;// 当前页（从0开始）
	private int pageSize = 10;
	@Override
	public void setRootView() {
		super.setRootView();
		setContentView(R.layout.activity_wonderful);
	}

	@Override
	public void initWidget() {
		super.initWidget();
		initTop();
		adapter=new WonderfulAppAdapter(aty);
		xlistView.setAdapter(adapter);
		xlistView.setXListViewListener(this);
		if(NetWorkUtils.getAPNType(aty)==0){
			xlistView.setVisibility(View.GONE);
			NoNetWork.setVisibility(View.VISIBLE);
		}else{
			xlistView.setVisibility(View.VISIBLE);
			NoNetWork.setVisibility(View.GONE);
			http();
		}
		NoNetWork.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if(NetWorkUtils.getAPNType(aty)==0){
					xlistView.setVisibility(View.GONE);
					NoNetWork.setVisibility(View.VISIBLE);
					ToastUtil.showShort(aty, "断网啦！");
				}else{
					xlistView.setVisibility(View.VISIBLE);
					NoNetWork.setVisibility(View.GONE);
					http();
				}
			}
		});
	}

	@Override
	public void widgetClick(View v) {
		super.widgetClick(v);
	}

	/**
	 * 更多精彩应用
	 */
	private void http() {
		HttpParams params = new HttpParams();
		params.put("current", page);// 当前页数(不传默认为1)
		params.put("rowCount", pageSize);// 每页显示数(不传默认为10)
		AppHttps.HttpPost(aty, HttpInterface.FIND_BY_LIST_APPLY, params,
				new AppHttpCallBack<findByListApplyListVo>(
						findByListApplyListVo.class, aty) {
					@Override
					public void onSuccess(findByListApplyListVo result) {
						boolean hasMore = true;
						if (refresh) {
							adapter.changeData(result.info);
						} else {
							adapter.addItems(result.info);
							adapter.notifyDataSetChanged();
						}
						if (result.info.size() < pageSize) {
							hasMore = false;
						}
						xlistView.setPullLoadEnable(hasMore);
					}

					@Override
					public void onFailures(int errorCode, String errorMsg) {
						ToastUtil.showShort(aty, errorMsg);
					}
					@Override
					public void onFinish() {
						super.onFinish();
						xlistView.setRefreshTime(CommUtil.formatDate(System
								.currentTimeMillis()));
					}
				});

	}

	private void initTop() {
		TopView topview = (TopView) findViewById(R.id.topview);
		topview.setTitle("更多精彩应用");
		topview.setLeftVisibility(View.GONE);
		topview.setLeftVisibilityExit(View.VISIBLE);
		topview.setRightVisibility(View.INVISIBLE);
		topview.setLeftOnClickExit(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
	}

	@Override
	public void onRefresh() {
		page=1;
		refresh = true;
		xlistView.setPullLoadEnable(false);
		http();
	}
	@Override
	public void onLoadMore() {
		page++;
		refresh = false;
		http();
	}
}
